package com.food.misc;

import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.food.model.Manager;
import com.food.model.Member;

/**
 * Servlet Filter implementation class LoginFilter
 */
@WebFilter({ "/member/MyCollect", "/recipe/writerecipe.jsp","/member/MyRecipe","/ApplyCourseServlet","/recipe/CollectServlet","/member/OrderServlet","/recipe/MessageServlet","/shopping/DisplayProduct","/recipe/GradeServlet","/course/application.jsp" })
public class LoginFilter implements Filter {

	public void destroy() {

	}
	public void doFilter(ServletRequest req,
			ServletResponse resp, FilterChain chain) throws IOException, ServletException {
		
		HttpServletRequest request = (HttpServletRequest) req;
		HttpServletResponse response = (HttpServletResponse) resp;
		
		HttpSession session = request.getSession();
		Member bean = 
				(Member) session.getAttribute("user");
		Manager manager = (Manager) session.getAttribute("manager");
		
		if(bean!=null || manager!=null) {
			chain.doFilter(request, response);
		} else {
			String uri = request.getRequestURI();
			session.setAttribute("dest", uri);
			String warn = "請先登入會員!";
			session.setAttribute("warn",warn );
			if(request.getParameter("cid")!=null){
				session.setAttribute("cid",request.getParameter("cid") );
			}
			
			String path = request.getContextPath();
			response.sendRedirect(path+"/login/login.jsp");
		}
	}
	private FilterConfig config ;
	public void init(FilterConfig config) throws ServletException {
		this.config = config;
	}

}
